package org.boulderfoodrescue.webapp.client.pickuptimes;

import java.util.Date;

import org.boulderfoodrescue.webapp.client.resources.Resources;
import org.boulderfoodrescue.webapp.shared.OneTimeEvent;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.view.client.ListDataProvider;

public class TimeSlotsTableOneTime extends FlowPanel{
	protected CellTable<OneTimeEvent> table;
	protected ListDataProvider<OneTimeEvent> dataProvider;
	public TimeSlotsTableOneTime(){
		dataProvider = new ListDataProvider<OneTimeEvent>();
		table = new CellTable<OneTimeEvent>();

		DateCell startTimeCell = new DateCell(DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.TIME_SHORT));
		Column<OneTimeEvent, Date> startTimeColumn = new Column<OneTimeEvent, Date>(startTimeCell){
			@Override
			public Date getValue(OneTimeEvent object) {
				return object.getStartTime();
			}
		};

		DateCell endTimeCell = new DateCell(DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.TIME_SHORT));
		Column<OneTimeEvent, Date> endTimeColumn = new Column<OneTimeEvent, Date>(endTimeCell){
			@Override
			public Date getValue(OneTimeEvent object) {
				return object.getEndTime();
			}
		};
		
		DateCell startDateCell = new DateCell(DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT));
		Column<OneTimeEvent, Date> startDateColumn = new Column<OneTimeEvent, Date>(startDateCell){
			@Override
			public Date getValue(OneTimeEvent object) {
				return object.getStartTime();
			}
		};
		
		DateCell endDateCell = new DateCell(DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT));
		Column<OneTimeEvent, Date> endDateColumn = new Column<OneTimeEvent, Date>(endDateCell){
			@Override
			public Date getValue(OneTimeEvent object) {
				return object.getEndTime();
			}
		};

		ClickableTextCell editCell = new ClickableTextCell();
		Column<OneTimeEvent, String> editColumn = new Column<OneTimeEvent, String>(editCell){
			@Override
			public String getValue(OneTimeEvent object) {
				return "delete";
			}
		};
		editColumn.setCellStyleNames(Resources.INSTANCE.ops().textButtonCell());

		table.addColumn(startTimeColumn, "Start Time");
		table.addColumn(endTimeColumn, "End Time");
		table.addColumn(startDateColumn, "Start Date");
		table.addColumn(endDateColumn, "End Date");
		table.addColumn(editColumn, "Edit");

		dataProvider.addDataDisplay(table);

		ListHandler<OneTimeEvent> sortHandler = new ListHandler<OneTimeEvent>(dataProvider.getList());
		table.addColumnSortHandler(sortHandler);

		editColumn.setFieldUpdater(new FieldUpdater<OneTimeEvent, String>(){
			@Override
			public void update(int index, final OneTimeEvent object, String value) {
				Window.alert("To Do");
			}
		});

		this.add(table);
		table.setWidth("100%");
		table.setEmptyTableWidget(new HTML("No time slots"));
	}
	public void addOneTimeEvent(OneTimeEvent event){
		dataProvider.getList().add(event);
	}
	public void removeOneTimeEvent(OneTimeEvent event){
		dataProvider.getList().remove(event);
	}
	public void refresh()
	{
		table.redraw();
	}
}
